<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页 - 宠物寄养平台-后台管理系统</title>
<jsp:include page="headlink.jsp"></jsp:include>
</head>

<body>
	<div class="lyear-layout-web">
		<div class="lyear-layout-container">
			<!--左侧导航-->
			<jsp:include page="leftsidebar.jsp"></jsp:include>
			<!--End 左侧导航-->

			<!--头部信息-->
			<jsp:include page="header.jsp"></jsp:include>
			<!--End 头部信息-->

			<!--页面主要内容-->
			<main class="lyear-layout-content">

			<div class="container-fluid">

				<div class="demoTable">
					搜索姓名：
					<div class="layui-inline">
						<input class="layui-input" name="id" id="demoReload"
							autocomplete="off">
					</div>
					<button class="layui-btn" data-type="reload">搜索</button>
				</div>
				<table class="layui-hide" id="test" lay-filter="test"></table>

				<!-- 添加按钮弹出层 -->
				<div class="layui-row" id="adduserinfo" style="display: none;">
					<div class="layui-col-md11">
						<form class="layui-form" action="" id='addmyform'>
							<div class="layui-form-item">
								<label class="layui-form-label">用户名：</label>
								<div class="layui-input-inline">
									<input type="text" name="uname" class="layui-input"
										id="adduserName" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">密码：</label>
								<div class="layui-input-inline">
									<input type="text" name="upwd" class="layui-input"
										id="adduserPwd" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">性别：</label>
								<div class="layui-input-block">
									<input type="radio" name="usex" value="男" title="男"
										checked=true id="adduserSexMan"> <input type="radio"
										name="usex" value="女" title="女" id="adduserSexWoman">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">手机号码：</label>
								<div class="layui-input-inline">
									<input type="tel" name="utel" lay-verify="required|phone"
										autocomplete="off" class="layui-input" id="adduserTel"
										value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">地址：</label>
								<div class="layui-input-inline">
									<input type="text" name="uaddress" class="layui-input"
										id="adduserAddress" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">等级：</label>
								<div class="layui-input-block">
									<select name="ulevel" lay-filter="level" id="adduserLevel">
										<option value="">请选择</option>
										<option value="1" id="addmanager">普通员</option>
										<option value="2" id="addcomment" selected>寄养家庭</option>
										<option value="3" id="addmanager">管理员</option>
									</select>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">状态：</label>
								<div class="layui-input-block">
									<select name="ustatus" lay-filter="state" id="adduserState">
										<option value="">请选择</option>
										<option value="0" id="addoff">禁用</option>
										<option value="1" id="addon" selected>启用</option>
									</select>
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal"
										id="addsubbtn" lay-submit="" lay-filter="demo2">提交</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<!-- 添加按钮弹出层 -->
				<!-- 编辑按钮弹出层 -->
				<div class="layui-row" id="edituserinfo" style="display: none;">
					<div class="layui-col-md11">
						<form class="layui-form" action="" id='myform'>
							<div class="layui-form-item">
								<label class="layui-form-label">用户编号：</label>
								<div class="layui-input-inline">
									<input type="text" name="uid" class="layui-input" id="userId"
										value="" readonly>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">用户名：</label>
								<div class="layui-input-inline">
									<input type="text" name="uname" class="layui-input"
										id="userName" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">密码：</label>
								<div class="layui-input-inline">
									<input type="text" name="upwd" class="layui-input" readonly
										id="userPwd" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">性别：</label>
								<div class="layui-input-block">
									<input type="radio" name="usex" value="男" title="男"
										checked=true id="userSexMan"> <input type="radio"
										name="usex" value="女" title="女" id="userSexWoman">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">手机号码：</label>
								<div class="layui-input-inline">
									<input type="tel" name="utel" lay-verify="required|phone"
										autocomplete="off" class="layui-input" id="userTel" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">地址：</label>
								<div class="layui-input-inline">
									<input type="text" name="uaddress" class="layui-input"
										id="userAddress" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">等级：</label>
								<div class="layui-input-block">
									<select name="ulevel" lay-filter="level" id="userLevel">
										<option value="">请选择</option>
										<option value="0" id="manager">管理员</option>
										<option value="1" id="comment" selected>普通用户</option>
									</select>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">状态：</label>
								<div class="layui-input-block">
									<select name="ustatus" lay-filter="state" id="userState">
										<option value="">请选择</option>
										<option value="0" id="off">禁用</option>
										<option value="1" id="on" selected>启用</option>
									</select>
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal"
										id="subbtn" lay-submit="" lay-filter="demo1">提交</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<!-- 编辑按钮弹出层结束 -->

			</div>

			</main>
			<!--End 页面主要内容-->
		</div>
	</div>

	<script type="text/javascript" src="${pageContext.request.contextPath}/backmanage/js/jquery.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/backmanage/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/backmanage/js/perfect-scrollbar.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/backmanage/js/main.min.js"></script>
	<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">批量删除</button>
    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
	<button class="layui-btn layui-btn-sm" lay-event="adduser">添加用户</button>
  </div>
</script>

	<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  	{{# if (d.ustatus=== 1) { }}  
		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="forBind">禁用</a>  
	{{# } else { }}  
		<a class="layui-btn layui-btn-xs" lay-event="forBind">启用</a>
	{{# } }}  


</script>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="bind">禁用</a>
	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="band">启用</a>
	<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
	<script src="${pageContext.request.contextPath}/backmanage/layui/layui.js" charset="utf-8"></script>
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

	<script>
		layui.use('table', function() {
			var table = layui.table;
			var form = layui.form;

			table.render({
				elem : '#test',
				url : '../users',
				toolbar : '#toolbarDemo',
				title : '用户数据表',
				cols : [ [ {
					type : 'checkbox',
					fixed : 'left'
				}, {
					field : 'uid',
					title : '用户编号',
					width : 120,
					sort : true,
					fixed : 'left'
				}, {
					field : 'uname',
					title : '用户名',
					width : 120
				}, {
					field : 'upwd',
					title : '密码',
					width : 280,
					sort : true
				}, {
					field : 'usex',
					title : '性别',
					width : 80
				}, {
					field : 'utel',
					title : '电话号码',
					width : 177
				}, {
					field : 'uaddress',
					title : '地址',
					width : 120,
					sort : true
				}, {
					field : 'ulevel',
					title : '等级',
					width : 120,
					sort : true,
					templet : function(res) {
						if (res.ulevel == 1) {
							return "普通用户";
						} else if (res.ulevel == 2) {
							return "寄养家庭";
						} else {
							return "管理员";
						}

					}
				}, {
					field : 'ustatus',
					title : '状态',
					width : 80,
					templet : function(res) {
						return res.ustatus == 1 ? "启用" : "禁用"

					}
				}, {
					fixed : 'right',
					title : '操作',
					toolbar : '#barDemo',
					width : 200
				} ] ],
				page : true,
				id : 'testReload'
			});

			//头工具栏事件
			table.on('toolbar(test)', function(obj) {
				var checkStatus = table.checkStatus(obj.config.id);
				switch (obj.event) {
				case 'getCheckData':
					var data = checkStatus.data;
					//layer.alert(JSON.stringify(data));
					if(data==""){
						layer.msg('至少选中一个',{icon:2});
					}
                    var ids="";
                     if(data.length>0){
                    	for(var i=0;i<data.length;i++){
                    		if(data[i].uid=="${sessionScope.user.uid}"){
                    			layer.msg('有账户在登录无法删除',{icon:2});
                    			return;
                    		}else{
                    			ids+=data[i].uid+",";//拼接id
                    		}
                    	}
                    	
                    	layer.confirm('真的删除这'+data.length+'条记录吗', function(index) {
							$.ajax({
								'type' : 'post',
								'url' : '../deletes' ,
								 'data':{"ids":ids},
								'success' : function(data) {
									if (data == true) {
										layer.closeAll();
										table.reload('testReload');//重新加载列表
										layer.msg("删除成功")
									}else{
										layer.closeAll();
										table.reload('testReload');//重新加载列表
										layer.msg("已有订单未完成，删除失败")
									}
								}
							});
                    	});
                     }
					break;
				case 'getCheckLength':
					var data = checkStatus.data;
					layer.msg('选中了：' + data.length + ' 个');
					break;
				case 'isAll':
					layer.msg(checkStatus.isAll ? '全选' : '未全选');
					break;
				case 'adduser':
					layer.open({
						type : 1,
						title : "添加用户信息",
						skin : 'layui-layer-rim', //加上边框
						area : [ '500px', '500px' ], //宽高
						content : $("#adduserinfo"),
					});
					/* 渲染表单 */
					form.render();
					break;
				}
				;
			});

			//监听行工具事件
			table.on('tool(test)', function(obj) {
				var data = obj.data;
				var uid = data.uid;
				var uname = data.uname;
				var upwd = data.upwd;
				var usex = data.usex;
				var utel = data.utel;
				var uaddress = data.uaddress;
				var ulevel = data.ulevel;
				var ustatus = data.ustatus;
				if (obj.event === 'del') {
					layer.confirm('真的删除吗？', function(index) {
						if ("${sessionScope.user.uname}" != data.uname) {
							$.ajax({
								'type' : 'delete',
								'url' : '../delete/' + data.uid+'/'+data.uname,
								'success' : function(data) {
									if (data == true) {
										layer.closeAll();
										table.reload('testReload');//重新加载列表
										layer.msg("删除成功")
									}else{
										layer.closeAll();
										table.reload('testReload');//重新加载列表
										layer.msg("已有订单未完成，删除失败")
									}
								}
							});
						} else {
							layer.msg('此用户正在登录')
						}
					})

				} else if (obj.event === 'edit') {
					$("#userId").attr("value", uid);
					$("#userName").attr("value", uname);
					$("#userPwd").attr("value", upwd);
					if (usex === "女") {
						$("#userSexWoman").prop("checked", true);
						$("#userSexMan").prop("checked", false);
					} else {
						$("#userSexMan").prop("checked", true);
						$("#userSexWoman").prop("checked", false);
					}

					$("#userTel").attr("value", utel);
					$("#userAddress").attr("value", uaddress);
					if (ulevel == 0) {
						$("#manager").attr("selected", true);
						$("#comment").attr("selected", false);
					} else {
						$("#comment").attr("selected", true);
						$("#manager").attr("selected", false);
					}

					if (ustatus == 0) {
						$("#on").attr("selected", false);
						$("#off").attr("selected", true);
					} else {
						$("#on").attr("selected", true);
						$("#off").attr("selected", false);
					}

					//页面层
					layer.open({
						type : 1,
						title : "修改用户信息",
						skin : 'layui-layer-rim', //加上边框
						area : [ '500px', '500px' ], //宽高
						content : $("#edituserinfo"),
					});
					/* 渲染表单 */
					form.render();
				} else if (obj.event === 'forBind') {
					if (ustatus == 1) {
						layer.confirm('确认禁用此账户吗？', function(index) {
							if ("${sessionScope.user.uname}" != data.uname) {
								$.ajax({
									'type' : 'post',
									'url' : '../band',
									'data' : JSON.stringify(data),
									'contentType' : 'application/json',
									'success' : function(data) {
										if (data == true) {
											layer.closeAll();
											table.reload('testReload');//重新加载列表
											$('fob').text('禁用');
										}
									}
								});
							}else{
								layer.msg('此用户正在登录,无法禁用')
							}	

						})
					} else {
						layer.confirm('确认启用此账户吗？', function(index) {
							$.ajax({
								'type' : 'post',
								'url' : '../band',
								'data' : JSON.stringify(data),
								'contentType' : 'application/json',
								'success' : function(data) {
									if (data == true) {
										layer.closeAll();
										table.reload('testReload');//重新加载列表
										console.log($('fob').text);
										$('fob').text('禁用');
									}
								}
							});
						})
					}

				}
			});

			var $ = layui.$, active = {
				reload : function() {
					var demoReload = $('#demoReload');

					//执行重载
					table.reload('testReload', {
						page : {
							curr : 1
						//重新从第 1 页开始
						},
						where : {
							content : demoReload.val()
						}
					});
				}
			};

			$('.demoTable .layui-btn').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});

			//编辑--监听提交
			layui.form.on('submit(demo1)', function(data) {
				/*  layer.alert(JSON.stringify(data.field), {
				   title: '最终的提交信息'
				 })
				 return false; */
				$.ajax({
					'type' : 'put',
					'url' : '../update',
					'data' : JSON.stringify(data.field),
					'contentType' : 'application/json',
					'success' : function(data) {
						if (data == true) {
							layer.closeAll();
							table.reload('testReload');//重新加载列表
							layer.msg("修改成功")
						}
					}
				});
				 form.render();
			});

			//添加--监听提交
			layui.form.on('submit(demo2)', function(data) {
				/*  layer.alert(JSON.stringify(data.field), {
				   title: '最终的提交信息'
				 })
				 return false; */
				$.ajax({
					'type' : 'post',
					'url' : '../insert',
					'data' : JSON.stringify(data.field),
					'contentType' : 'application/json',
					'success' : function(data) {
						if (data.msg === "注册成功") {
							layer.closeAll();
							table.reload('testReload');//重新加载列表
							layer.msg("添加成功")
						}
					}
				});
			});
		});
	</script>

</body>